package com.axones.planningpoker.dao;

import java.util.ArrayList;

import com.axones.planningpoker.bean.Item;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;

public class ItemDAO {
	private static final String KIND_NAME = "Item";
	
	/**
	 * @param item
	 */
	public Long add(Item item) {
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();

		return datastore.put(convert(item)).getId();
	}

	/**
	 * @param item
	 */
	public void delete(Long id) {
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();

		Key k = KeyFactory.createKey(KIND_NAME, id);

		datastore.delete(k);
	}
	
	/**
	 * @param id
	 */
	public void deleteByProject(Long id) {
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();

		Query q = new Query(KIND_NAME);
		q.addFilter("projectId", Query.FilterOperator.EQUAL, id);

		PreparedQuery pq = datastore.prepare(q);

		for (Entity pu : pq.asIterable()) {
			datastore.delete(pu.getKey());
		}
	}

	/**
	 * @param item
	 * @return
	 */
	public String update(Item item) {
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();

		Key k = KeyFactory.createKey(KIND_NAME, item.getId());

		Entity i;

		try {
			i = datastore.get(k);
		} catch (EntityNotFoundException e) {
			return "not.found";
		}

		update(item, i);

		datastore.put(i);

		return "ok";
	}

	/**
	 * @param projectId
	 * @return
	 */
	public ArrayList<Item> getAll(long projectId) {
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();

		Query q = new Query(KIND_NAME);
		q.addFilter("projectId", Query.FilterOperator.EQUAL, projectId);

		PreparedQuery pq = datastore.prepare(q);

		ArrayList<Item> items = new ArrayList<Item>();

		for (Entity i : pq.asIterable()) {
			items.add(convert(i));
		}

		return items;
	}

	/**
	 * @param i
	 * @return
	 */
	private Item convert(Entity i) {
		Item item = new Item();

		item.setId(i.getKey().getId());

		item.setProjectId((Long) i.getProperty("projectId"));
		item.setName((String) i.getProperty("name"));
		item.setValue(((Long) i.getProperty("value")).intValue());

		return item;
	}

	/**
	 * @param item
	 * @return
	 */
	private Entity convert(Item item) {
		Entity i = new Entity(KIND_NAME);

		i.setProperty("projectId", item.getProjectId());
		i.setProperty("name", item.getName());
		i.setProperty("value", item.getValue());

		return i;
	}

	/**
	 * @param item
	 * @param i
	 */
	private void update(Item item, Entity i) {
		i.setProperty("projectId", item.getProjectId());
		i.setProperty("name", item.getName());
		i.setProperty("value", item.getValue());
	}
}
